//
//  MineIntegralRootViewController.m
//  shopping
//
//  Created by 马田成 on 2017/8/31.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "MineIntegralRootViewController.h"
#import "MineIntergraiFitstTableViewCell.h"
#import "MineIntergraiSecondTableViewCell.h"
#import "FuTouFUCKView.h"
#import "MineJiFenMingXiViewController.h"//积分明细
#import "MineFenHongDetailViewController.h"

@interface MineIntegralRootViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{

    UIView *grayView;
    NSString *money;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) FuTouFUCKView *FuTouFUCKView;


@end

@implementation MineIntegralRootViewController
//#pragma mark - ---------- 懒加载 ----------
//- (FuTouFUCKView *)changeHeadImageView {
//    if (!_FuTouFUCKView)
//    {
//        _FuTouFUCKView = [[NSBundle mainBundle] loadNibNamed:@"FuTouFUCKView" owner:self options:nil].lastObject;
//    }
//    return _FuTouFUCKView;
//}

- (void)viewWillAppear:(BOOL)animated{
    
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self data];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self fuTouData];
    
    [self creationView];
    [self regeditCell];
    
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    [self setTitle:@"我的分红"];


    UIView *viewOf = [[UIView alloc] init];
    viewOf.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1);
    [self.view addSubview:viewOf];
    viewOf.backgroundColor = [UIColor colorWithRed:204 / 255.0f green:204 / 255.0f  blue:204 / 255.0f  alpha:1];
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;

    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    
   
    self.view.backgroundColor = [UIColor colorWithRed:245 / 255.0f green:245 / 255.0f blue:245 / 255.0f alpha:1];
    self.FuTouFUCKView.moneyTF.delegate = self;
    
    self.FuTouFUCKView.moneyTF.keyboardType = UIKeyboardTypeNumberPad;
    //添加注册按钮
    UIButton *buttonAdd = [[UIButton alloc] init];
    
    buttonAdd.frame = CGRectMake(DEF_SCREEN_WIDTH - 15, 0, 80, 40);
    [buttonAdd setTitle:@"分红明细" forState:UIControlStateNormal];
    [buttonAdd addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    UIColor * color = getColor(@"222222");
    [buttonAdd setTitleColor:color forState:UIControlStateNormal];
    buttonAdd.titleLabel.font = [UIFont systemFontOfSize:15];
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:buttonAdd];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    
}
- (void)registerClick{

    MineJiFenMingXiViewController *vc = [[MineJiFenMingXiViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (void)data{
    
    
    
    _dataArr = [NSMutableArray array];

    _dataDic = [[NSMutableDictionary alloc] init];
    
    
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    
    [productParams setValue:token forKey:@"token"];
    
    
    [[MNetworkHandler sharedInstance]conURL:adminUsermyIntegral networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
    
    
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
    
            NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
            
           dic = [returnData objectForKey:@"data"];
            
            _dataArr = [dic objectForKey:@"product"];
            _dataDic = [dic objectForKey:@"jifenchi"];
            
            
            NSLog(@"!!!!!!!!!!!!!!!%@", _dataArr);
     
            NSLog(@"!!!!!!!!!!!!!!!%@", _dataDic);
            [self.view addSubview:self.tableView];
            
            [self.tableView reloadData];
    
            
        }
    
        
        
        else {
    
                //                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
    
            
        }
    
        
    } failureBlock:^(NSError *error) {
    
            NSLog(@"%@",error);
    
//            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
    
        
    }];
    
    
    
    
}
- (void)fuTouData{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    
    [productParams setValue:token forKey:@"token"];
    
    
    [[MNetworkHandler sharedInstance]conURL:adminUserjiaoyimoney networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            NSMutableDictionary *dic =[[NSMutableDictionary alloc] init];
            
            dic = [returnData objectForKey:@"data"];
            money = [dic objectForKey:@"money_jiaoyi"];
            
            
        }
        
        
        
        else {
            
            //                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
            
        }
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
        
    }];


}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineIntergraiFitstTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineIntergraiFitstTableViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineIntergraiSecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineIntergraiSecondTableViewCell"];
    
    
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else {
    
        return self.dataArr.count;
    }
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        return 180;
    }else {
    
        return 110;
    }
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0) {
    
        MineIntergraiFitstTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MineIntergraiFitstTableViewCell" forIndexPath:indexPath];
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        [cell.futouButton addTarget:self action:@selector(futouView) forControlEvents:UIControlEventTouchUpInside];
        
        
        cell.jiFenRL.text = [self.dataDic objectForKey:@"integral_capacity"];
        cell.yiFanJiF.text = [self.dataDic objectForKey:@"returned_integral"];
        cell.daiFanJF.text =[self.dataDic objectForKey:@"last_integral"];
        
     //   [self.dataDic objectForKey:@"last_integral"];
        
        CGFloat num1 = [[self.dataDic objectForKey:@"bili"] intValue];//返的积分
        
        [cell.profressViewsss setProgress:num1 animated:YES];
    
        
        
        return cell;
    }else {
        MineIntergraiSecondTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MineIntergraiSecondTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        NSMutableDictionary *dic = self.dataArr[indexPath.row];
        cell.all_jifen.text = [dic objectForKey:@"all_jifen"];
        cell.goodname.text = [dic objectForKey:@"goodname"];
        cell.addtime.text = [dic objectForKey:@"addtime"];
        NSString *str1 = @"已返天数";
        cell.release_day.text = [str1 stringByAppendingString:[dic objectForKey:@"release_day"]];
        NSString *str2 = @"已返";
        cell.shifang_jifen.text = [str2 stringByAppendingString:[dic objectForKey:@"shifang_jifen"]];
        

        return cell;
        
        
    }
    
}

#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 1) {
        
        MineFenHongDetailViewController *vc = [[MineFenHongDetailViewController alloc] init];
        NSMutableDictionary *dic = self.dataArr[indexPath.row];
        vc.fanli_id = [dic objectForKey:@"id"];
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }
    
    
}


- (void)futouView{

    grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
    grayView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:0 / 255.0f blue:0 / 255.0f alpha:0.2];
    
    [[UIApplication sharedApplication].keyWindow addSubview:grayView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView)];
    [grayView addGestureRecognizer:tapGesture];
    _FuTouFUCKView = [[NSBundle mainBundle] loadNibNamed:@"FuTouFUCKView" owner:self options:nil].lastObject;
    [grayView addSubview:self.FuTouFUCKView];
    self.FuTouFUCKView.backgroundColor = [UIColor whiteColor];
    [self.FuTouFUCKView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(150);
        make.height.offset(208);
        make.width.offset(260);
        make.centerX.equalTo(self.view.mas_centerX);
        
    }];
    self.FuTouFUCKView.yuELabel.text = money;
    [self.FuTouFUCKView.quXiaoBtn addTarget:self action:@selector(dismissContactView) forControlEvents:UIControlEventTouchUpInside];
    
    [self.FuTouFUCKView.queRenBtn addTarget:self action:@selector(queRenCLick) forControlEvents:UIControlEventTouchUpInside];
    

    
}
- (void)dismissContactView {
    [grayView removeFromSuperview];
}
- (void)queRenCLick{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:self.FuTouFUCKView.moneyTF.text forKey:@"money"];
    
    
    [[MNetworkHandler sharedInstance]conURL:adminUserRecast networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
            [self data];
            [self dismissContactView];
            
        }
        
        
        
        else {
            
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
            
        }
        
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
//        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
        
    }];

    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
